package com.it.controller;

import com.alibaba.fastjson.JSONObject;
import com.it.common.Result;
import com.it.domain.Singer;
import com.it.domain.Song;
import com.it.domain.SongSinger;
import com.it.service.SingerService;
import com.it.service.SongService;
import com.it.common.CONSTS;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RestController
@RequestMapping("/Song")
public class SongController {

    @Autowired
    private SongService songService;

    @Autowired
    private SingerService singerService;

    @PostMapping("/AddSong")
    public Object AddSinger(@RequestBody Song song) {
        System.out.println(song.getLyric());
        System.out.println(song);
        String name = song.getName().trim();
        Integer singerId = song.getSingerId();
        String album = song.getAlbum().trim();
        String url = song.getUrl().trim();
        String image = song.getImage().trim();
        Date createTime = song.getCreateTime();
        String lyric = song.getLyric().trim();

        Song song1 = new Song();
        song1.setImage(image);
        song1.setName(name);
        song1.setCreateTime(createTime);
        song1.setAlbum(album);
        song1.setLyric(lyric);
        song1.setUrl(url);
        song1.setSingerId(singerId);

        return songService.AddSong(song1);
    }

    @PostMapping("/SelectAll")
    public Map<String,Object> SelectAll(@RequestParam("PageNum") Integer PageNum, @RequestParam("PageSize")
    Integer PageSize,@RequestParam("singerId") Integer singerId,@RequestParam("name") String name){

        System.out.println(PageNum);
        System.out.println(PageSize);
        System.out.println(singerId);
        PageNum = (PageNum-1)*PageSize;
        name = "%"+name+"%";
        Map<String,Object> res = new HashMap<String,Object>();
        if(singerId == null){
            return null;
        }
        Integer total = songService.SelectSingerSongTotal(singerId);
        List<Song> data = songService.SelectAll(PageNum,PageSize,singerId,name);
        System.out.println(total);
        res.put("total",total);
        res.put("data",data);
        return res;
    }

    @DeleteMapping("/DeleteSong/{id}")
    public Object DeleteSong(@PathVariable Integer id){
        JSONObject jsonObject = new JSONObject();
        boolean flag = songService.DeleteSong(id);
        if (flag) {
            jsonObject.put(CONSTS.CODE, 200);
            jsonObject.put(CONSTS.MSG, "删除成功");
        } else {
            jsonObject.put(CONSTS.CODE, 500);
            jsonObject.put(CONSTS.MSG, "删除失败");
        }
        return jsonObject;
    }

    @PutMapping("/UpdateSong")
    public Object UpdateSong(@RequestBody Song song){
        System.out.println(song.getLyric());
        JSONObject jsonobject = new JSONObject();
        Integer id = song.getId();
        String name = song.getName().trim();
        String image = song.getImage().trim();
        String lyric = song.getLyric().trim();
        String album = song.getAlbum().trim();
        Date createTime = song.getCreateTime();
        System.out.println("Time===");
        System.out.println(createTime);
        String url = song.getUrl().trim();
        Integer singerId = song.getSingerId();

        Song song1 = new Song();
        song1.setSingerId(singerId);
        song1.setUrl(url);
        song1.setImage(image);
        song1.setLyric(lyric);
        song1.setAlbum(album);
        song1.setId(id);
        song1.setName(name);
        song1.setUpdateTime(createTime);

        boolean flag = songService.UpdateSong(song1);
        if (flag) {
            jsonobject.put(CONSTS.CODE, 200);
            jsonobject.put(CONSTS.MSG, "更新成功");
        } else {
            jsonobject.put(CONSTS.CODE, 400);
            jsonobject.put(CONSTS.MSG, "更新失败");
        }
        return jsonobject;
    }

    @GetMapping("/SelectBySongId/{id}")
    public Song SelectBySongId(@PathVariable Integer id) {
        return songService.SelectBySongId(id);
    }

    @GetMapping("/GetAllSong")
     public List<Song> GetAllSong(){
         return songService.GetAllSong();
    }

    @PostMapping("/SelectByName")
    public List<SongSinger> SelectByName(@RequestParam("name") String name){

        name = "%" + name + '%';
        System.out.println(name);
        Map<String,Object> res = new HashMap<String,Object>();
        List<Song> data = songService.SelectByName(name);
        List<SongSinger> songsinger = new ArrayList<>();

        for(Song song : data){
            SongSinger songSinger = new SongSinger();
            songSinger.setName(song.getName());
            songSinger.setImage(song.getImage());
            songSinger.setAlbum(song.getAlbum());
            songSinger.setUrl(song.getUrl());
            songSinger.setSongId(song.getId());
            songSinger.setLyric(song.getLyric());
            Singer singer = new Singer();
            if(song.getSingerId() == null){
                continue;
            }
            singer = singerService.SelectBid(song.getSingerId());
            songSinger.setSinger(singer.getName());
            songsinger.add(songSinger);
        }
        return songsinger;
    }

    @PostMapping("/ChangeType")
    public Result UpdateType(@RequestParam Integer id, @RequestParam Integer isDelete){
        if(songService.UpdateType(id,isDelete)>0){
            return Result.success("200","操作失败");
        }
        else{
            return Result.fail("500","操作失败");
        }
    }

    @GetMapping("/getDataTotal/{singerId}")
    public Result getDataTotal(@PathVariable Integer singerId){

        Map<String,Object> map = new HashMap<>();
        Integer songsTotal = songService.getSongsTotal(singerId);
        Integer AlbumTotal = songService.getAlbumTotal(singerId);
        map.put("songsTotal",songsTotal);
        map.put("AlbumTotal",AlbumTotal);
        return Result.getinfo("200",map);
    }

    @GetMapping("/getSongInfo/{singerId}")
    public List<SongSinger> getSongInfo(@PathVariable Integer singerId){
        List<Song> songs = songService.getSongInfo(singerId);
        List<SongSinger> songSingers = new ArrayList<>();
        for(Song song : songs){
            SongSinger songSinger = new SongSinger();
            songSinger.setName(song.getName());
            songSinger.setImage(song.getImage());
            songSinger.setAlbum(song.getAlbum());
            songSinger.setUrl(song.getUrl());
            songSinger.setSongId(song.getId());
            songSinger.setLyric(song.getLyric());
            System.out.println(song.getSinger());
            songSinger.setSinger(song.getSinger().getName());
            songSingers.add(songSinger);
        }
        return songSingers;
    }

    @GetMapping("/SelectToTalSong")
    public Integer SelectToTalSong(){
        return songService.SelectTotal();
    }
}
